<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;
use think\middleware\SessionInit;
use think\middleware\Throttle;
use app\middleware\api\admin\ApiTokenCheck;
use app\middleware\api\admin\ApiAuthCheck;


Route::miss('api.index.Index/miss');

Route::get('/', 'api.index.Index/index');

Route::group('api', function () {

    Route::group('admin', function () {
        Route::post('login', 'api.admin.Login/login')
            ->middleware(Throttle::class, [
                'visit_rate' => '300/720',
                'key' => '__CONTROLLER__/__ACTION__/__IP__',
            ])->middleware(SessionInit::class);

        Route::post('refresh-token', 'api.admin.Login/refreshToken');

        // token
        Route::group('', function () {

            Route::get('get-routes', 'api.admin.Login/getRoutes');

            Route::group('system', function () {
                Route::resource('menu', 'api.admin.system.Menu')->only(['index', 'save', 'delete', 'update']);
                Route::resource('dept', 'api.admin.system.Dept')->only(['index', 'save', 'delete', 'update']);
                Route::group('role', function () {
                    Route::resource('', 'api.admin.system.Role')->only(['index', 'save', 'delete', 'update', 'read']);
                    Route::put('rules/<id>', 'api.admin.system.Role/setRules');
                    Route::put('data-scope/<id>', 'api.admin.system.Role/setDataScope');
                    Route::put('change-dept', 'api.admin.system.Role/changeDept');
                });
                Route::group('user', function () {
                    Route::resource('', 'api.admin.system.User')->only(['index', 'save', 'delete', 'update', 'read']);
                    Route::get('mine', 'api.admin.system.User/mine');
                    Route::put('mine', 'api.admin.system.User/mineUpdate');
                    Route::put('change-pwd', 'api.admin.system.User/changePwd');
                    Route::get('data-scope', 'api.admin.system.User/dataScope');
                });
            });

            Route::group('general', function () {
                Route::resource('dict', 'api.admin.general.Dict')->only(['index', 'save', 'delete', 'update', 'read']);
                Route::resource('attachment', 'api.admin.general.attachment')->only(['index', 'delete']);
                Route::resource('print-template', 'api.admin.general.PrintTemplate')->only(['index', 'save', 'delete', 'update', 'read']);
                Route::resource('request-log', 'api.admin.general.RequestLog')->only(['index']);
                Route::resource('sms-log', 'api.admin.general.SmsLog')->only(['index']);
            });

        })
            ->middleware([
                ApiTokenCheck::class,
                ApiAuthCheck::class,
            ]);
    });

    Route::group('out', function () {
        Route::group('aid', function () {
            Route::post('upfile', 'api.Upload/uploadOut');
        });

        Route::get('captcha/[:config]', 'api.index.Index/captcha')
            ->middleware(Throttle::class, [
                'visit_rate' => '300/720',
                'key' => '__CONTROLLER__/__ACTION__/__IP__',
            ])->middleware(SessionInit::class);
    });
})->pattern(['id' => '\d+']);